package com.common.boot.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * ObjectMapper
 * @author xuqq
 * @date 2021-01-19
 * @version v1.0
 */
public class ObjectMapperUtil {
    public static <T> T parse(String str, Class<T> clazz) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            return objectMapper.readValue(str, clazz);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static <T> T convertValue(Object object,TypeReference<T> typeReference) {
            ObjectMapper objectMapper = new ObjectMapper();
            return objectMapper.convertValue(object,typeReference);
    }

    public static <T> T convertValue(Object object,Class<T> typeReference) {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.convertValue(object,typeReference);
    }

}
